import db from '@dfeidao/atom-nodejs/db';
import dt from '@dfeidao/atom-nodejs/dt';
import log from '@dfeidao/atom-nodejs/logger/log';
import { IncomingHttpHeaders } from 'http';
import { systemid } from '../atom/config';
interface Message {
	productid: string;
	leave_num: string;
}

interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string;
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}

export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	log('Service begin path:app-approval-details/s001,action_id:' + action_id);

	log('Service end path:app-approval-details/s001,action_id:' + action_id);
	const { exec, prepare } = db.query(action_id, session_id, systemid);
	prepare('student_leave', ['student_no', 'student_name', 'auditor_status', 'start_time', 'finish_time', 'leave_days', 'leave_content', 'leave_num', 'parent_number', 'parent_name'], { productid: message.productid, leave_num: message.leave_num }, 199, 1, [], []);
	const [data] = await exec();
	data.forEach((v: { start_time: number; finish_time: number; start_time_str: string; finish_time_str: string; auditor_status: string; }) => {
		v.start_time_str = dt.format(v.start_time, 'YYYY:MM:DD aa');
		v.finish_time_str = dt.format(v.finish_time, 'YYYY:MM:DD aa');
		if (v.auditor_status === '0') {
			v.auditor_status = '待审核';
		} else if (v.auditor_status === '1') {
			v.auditor_status = '已通过';
		} else if (v.auditor_status === '2') {
			v.auditor_status = '未通过';
		}
	});
	return {
		data: { data }
	};
}
